package main

import (
	"errors"
	"fmt"
)

var ErrSentinel = errors.New("the underlying sentinel error")

type MyError struct {
	e string
}

func (e *MyError) Error() string {
	return e.e
}

func main() {
	err1 := fmt.Errorf("warp sentinel: %w", ErrSentinel)
	err2 := fmt.Errorf("wrap err1: %w", err1)

	if errors.Is(ErrSentinel, err2) {
		println("ErrSentinel is err2")
	}

	println(err2 == ErrSentinel)      // false
	if errors.Is(err2, ErrSentinel) { // true
		println("err2 is ErrSentinel")
	} else {
		println("err2 is not ErrSentinel")
	}

	var err = &MyError{"MyError error demo"}
	err1 = fmt.Errorf("wrap err: %w", err)
	err2 = fmt.Errorf("wrap err1: %w", err1)
	var e *MyError
	if errors.As(err2, &e) {
		println("MyError is on the chain of err2")
		println(e == err)
		return
	} else {
		println("MyError is not on the chain of err2.")
	}
}
